#ifndef _BITREE_H_
#define _BITREE_H_


#include <stdlib.h>

typedef struct bitree_node {
	void						*data;
	struct bitree_node 		*left;
	struct	bitree_node		*right;
}bitree_node_t;

typedef struct bitree {
	int 	size;
	int		(*compare)(const void *key1, const void *key2);
	void	(*destroy)(void *data);

	bitree_node_t	*root;
}bitree_t;

void bitree_init(bitree_t *tree, void (*destroy)(void *data));
void bitree_destroy(bitree_t *tree);
int bitree_insert_left(bitree_t *tree, bitree_node_t *node, const void *data);
int bitree_insert_right(bitree_t * tree, bitree_node_t * node, const void * data);
void bitree_remove_left(bitree_t *tree, bitree_node_t *node);
void bitree_remove_right(bitree_t *tree, bitree_node_t *node);
int bitree_merge(bitree_t *merge, bitree_t *left, bitree_t *right, const void *data);

#define bitree_size(tree)	((tree)->size)
#define bitree_root(tree)	((tree)->root)
#define bitree_eob(node)	((node) == NULL)
static int inline bitree_is_leaf(const bitree_node_t *node)
{
	if (!node)
		return 0;
	else
		return (node->left == NULL && node->right == NULL);
}
								
static inline void *bitree_data (const bitree_node_t *node)
{
	if (!node)
		return NULL;
	else
		return node->data;
}
static inline bitree_node_t *bitree_left(const bitree_node_t *node)
{
	if (!node)
		return NULL;
	else
		return node->left;
}
static inline bitree_node_t *bitree_right(const bitree_node_t *node)
{
	if (!node)
		return NULL;
	else
		return node->right;
}


#endif /*_BITREE_H_*/

